package cn.stylefeng.guns.modular.merchandise.controller;

import cn.stylefeng.guns.core.common.constant.factory.ConstantFactory;
import cn.stylefeng.guns.modular.system.warpper.MerWrapper;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.util.ToolUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import cn.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import cn.stylefeng.guns.modular.system.model.Mer;
import cn.stylefeng.guns.modular.merchandise.service.IMerService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 商品管理控制器
 *
 * @author fengshuonan
 * @Date 2020-04-14 18:30:57
 */
@Controller
@RequestMapping("/mer")
public class MerController extends BaseController {

    private String PREFIX = "/mall/mer/";

    @Autowired
    private IMerService merService;

    /**
     * 跳转到商品管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "mer.html";
    }

    /**
     * 跳转到添加商品
     */
    @RequestMapping("/mer_add")
    public String merAdd() {
        return PREFIX + "mer_add.html";
    }

    /**
     * 跳转到修改商品
     */
    @RequestMapping("/mer_update/{merId}")
    public String merUpdate(@PathVariable Integer merId, Model model) {
        Mer mer = merService.selectById(merId);
        model.addAttribute("item",mer);
        model.addAttribute("categoryName", ConstantFactory.me().getCategoryName(mer.getCategory()));
        LogObjectHolder.me().set(mer);
        return PREFIX + "mer_edit.html";
    }

    /**
     * 获取商品列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        if(ToolUtil.isNotEmpty(condition)){
            EntityWrapper<Mer> merEntityWrapper = new EntityWrapper<Mer>();
            Wrapper<Mer> mer = merEntityWrapper.like("mername","%" + condition + "%");
            List<Map<String, Object>> merList=  merService.selectMaps(mer);
            return new MerWrapper(merList).wrap();
        }else{
            return new MerWrapper(merService.selectMaps(null)).wrap();
        }

    }

    /**
     * 新增商品
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Mer mer) {
        mer.setCreatetime(new Date());
        mer.setId(0);
        merService.insert(mer);
        return SUCCESS_TIP;
    }

    /**
     * 删除商品
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer merId) {
        merService.deleteById(merId);
        return SUCCESS_TIP;
    }

    /**
     * 修改商品
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Mer mer) {
        mer.setCreatetime(new Date());
        merService.updateById(mer);
        return SUCCESS_TIP;
    }

    /**
     * 商品管理详情
     */
    @RequestMapping(value = "/detail/{merId}")
    @ResponseBody
    public Object detail(@PathVariable("merId") Integer merId) {
        return merService.selectById(merId);
    }
}
